#include "lc_op1_Operator.h"
#define LAGNIAPE_ROOT_DIR "/home/riche/"
#define BUFFER_SIZE 8192


void 
op1::handleMyPort(NetRequest * data, unsigned long dataSize)
{
	assert(data != NULL);

        char *buffer ; 
        char *buf=data->getBuffer();
	int rd;
	int length = 0;
	int doneRequest = 0;
        socket_t soc_id= data->getSocketID();

	do {
	  char *start = buf;
	  char *end;
	  for (int i=0;i< data->getNumBytes();i++) {
	    if (buf[i] == '\r') {
	      buf[i++] = 0;
	      buf[i] = 0; // Get rid of the \n
	      //DEBUG printf("%s\n", start);
	      if (length == 0) { // We're done...
		doneRequest = true;
		break;
	      }
	      else if(start[0]=='G' && start[1]=='E' && start[2]=='T'){
		start = strchr(start, ' ')+1;
		end = strchr(start+1, ' ');
		*end = 0;
		while (*end != 'H')
		  end++;
		while (*end != '/')
		  end++;
		end++;
		int major = *end-'0'; // HACK HACK HACK Assumes ASCII
		end+=2;
		int minor = *end-'0'; // HACK HACK HACK Assumes ASCII
		if (major != 1 || (minor > 1))
		  printf("Urm, HTTP version: %d.%d we may be in trouble...\n",
			 major, minor);
		if (major == 1 && minor == 0) {
		  //out.close = true;
		}
		
		if (*start == '/')
		  start++;
		
		buffer = strdup(start);
	      }
	      else if (start[0]=='C' && start[1]=='o' && start[2]=='n'&&
		       strstr(start, "close")) {
		//out.close = true;
	      }
	      start=buf+i+1;
	      length = 0;
	    }
	    else {
	      length++;
	    }
	  }
	} while (!doneRequest);
	
	if (!buffer) {
	  buffer = "/sys_error.html";
	  delete[] data->getBuffer();
	}
	
	
        NetRequest *out_data = new NetRequest(buffer,data->getSocketID(),strlen(buffer));
	assert(out_data != NULL);
	out(out_data, dataSize);
	delete[] data->getBuffer();
	delete data;
	return;
}


